package online.xybh.qa.controller;

import online.xybh.qa.dto.TeacherAndClassDTO;
import online.xybh.qa.model.Student;
import online.xybh.qa.service.StudentService;
import online.xybh.qa.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 22:02 2020/10/20
 * @Modified:
 */
@Controller
public class LoginController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/login")
    public String index(@RequestParam(value = "backUrl", defaultValue = "/index") String backUrl,
                        Model model){
        model.addAttribute("backUrl", backUrl);
        return "login";
    }

    @GetMapping("/login/student")
    public String loginStudent(@RequestParam(value = "backUrl", defaultValue = "/index") String backUrl,
                               HttpServletRequest request,
                               HttpSession session) {
        session.setAttribute("backUrl", backUrl);
        return "login_student";
    }

    @PostMapping("/login/student")
    public String checkStudent(@RequestParam("id") String id,
                               @RequestParam("password") String password,
                               HttpSession session) {
        Student student = studentService.checkStudent(id, password);
        if (student != null) {
            String url = (String) session.getAttribute("backUrl");
            if (url == null) {
                url = "/index";
            }
            session.setAttribute("user", student);
            session.setAttribute("userType",1);
            return "redirect:" + url;
        }
        return "redirect:/login/student";
    }

    @GetMapping("/login/teacher")
    public String loginTeacher(@RequestParam(value = "backUrl", defaultValue = "/index") String backUrl,
                               HttpSession session) {
        session.setAttribute("backUrl", backUrl);
        return "login_teacher";
    }

    @PostMapping("/login/teacher")
    public String checkTeacher(@RequestParam("id") String id,
                               @RequestParam("password") String password,
                               HttpSession session) {
        TeacherAndClassDTO teacher = teacherService.queryTeacherAndClassById(id, password);
        if (teacher.getTeacherId() != null) {
            String url = (String) session.getAttribute("backUrl");
            if (url == null) {
                url = "/index";
            }
            session.setAttribute("user", teacher);
            session.setAttribute("userType",2);
            return "redirect:" + url;
        }
        return "redirect:/login/teacher";
    }

    @GetMapping("/logout")
    public String logout(@RequestParam(value = "backUrl", defaultValue = "/index") String backUrl,
                         HttpSession session) {
        session.removeAttribute("user");
        session.removeAttribute("userType");
        return "redirect:" + backUrl;
    }
}
